﻿using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;

namespace server_anychat
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("==Any Chat Server==");
            int startport = 1988;
            var server = new light.http.server.httpserver();
            server.SetFailAction(on404);
            var path = "www";

            var fullpath = System.IO.Path.GetFullPath(path);
            //配置一个WWW文件夹

            Console.WriteLine("==WWW path=" + fullpath);
            server.SetHttpController("/www", new light.http.server.filecontroller(fullpath));
            server.SetWebsocketAction("/ws", ChatServer.OnWebSocketConnect);

            //初始化ChatServer
            ChatServer.Init();
            
            server.Start(startport);
            Console.WriteLine("==Websockat API at:ws://[ip]:" + startport+"/ws");
            Console.WriteLine("==WebApp at http://[ip]:"+ startport+"/www");

            while (true)
            {
                Console.ReadLine();
            }
        }
        static async Task on404(HttpContext context)
        {
            context.Response.StatusCode = 404;
            await context.Response.WriteAsync("you got a 404");
        }

    }
}
